﻿Public Class frmConfig

    Private Sub frmConfig_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitFrm()
    End Sub

    Private ReadOnly Property Settings() As TGeneralSettings
        Get
            Return frmMain.GeneralSettings
        End Get
    End Property

    Private Sub InitFrm()
        RemoveEvents()
        InitCboFont()
        cboFont.SelectedItem = Settings.FontName
        txtFontSize.Value = Settings.FontSize
        chkBold.Checked = Settings.FontBold
        chkItalic.Checked = Settings.FontItalic
        cmdBackColor.BackColor = Settings.BackColor
        cmdBackColor.ForeColor = Common.ReversedColor(Settings.BackColor)
        cmdForeColor.BackColor = Settings.ForeColor
        cmdForeColor.ForeColor = Common.ReversedColor(Settings.ForeColor)
        txtSample.Text = "123abcあいうえお"
        txtSample.Font = Settings.Font
        chkShowData.Checked = Settings.ShowData
        chkShowAll.Checked = Settings.ShowAll
        txtDefaultLimit.Text = Settings.DefaultLimit
        AddEvents()
    End Sub

    Private Sub RemoveEvents()
        RemoveHandler cmdBackColor.Click, AddressOf cmdBackColor_Click
        RemoveHandler cmdForeColor.Click, AddressOf cmdForeColor_Click
        RemoveHandler chkBold.CheckedChanged, AddressOf chkBold_CheckedChanged
        RemoveHandler chkItalic.CheckedChanged, AddressOf chkItalic_CheckedChanged
        RemoveHandler txtFontSize.ValueChanged, AddressOf txtFontSize_ValueChanged
        RemoveHandler cboFont.SelectedIndexChanged, AddressOf cboFont_SelectedIndexChanged
    End Sub
    Private Sub AddEvents()
        AddHandler cmdBackColor.Click, AddressOf cmdBackColor_Click
        AddHandler cmdForeColor.Click, AddressOf cmdForeColor_Click
        AddHandler chkBold.CheckedChanged, AddressOf chkBold_CheckedChanged
        AddHandler chkItalic.CheckedChanged, AddressOf chkItalic_CheckedChanged
        AddHandler txtFontSize.ValueChanged, AddressOf txtFontSize_ValueChanged
        AddHandler cboFont.SelectedIndexChanged, AddressOf cboFont_SelectedIndexChanged
    End Sub

    Private Sub InitCboFont()
        cboFont.Items.Clear()
        cboFont.DrawMode = DrawMode.OwnerDrawFixed
        cboFont.ItemHeight = 20
        AddHandler cboFont.DrawItem, AddressOf cboFont_DrawItem
        For i As Integer = 0 To Settings.FontCount - 1
            cboFont.Items.Add(Settings.Font(i).Name)
        Next
    End Sub

    Private Sub cboFont_DrawItem(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.DrawItemEventArgs)
        e.DrawBackground()
        Dim cmb As ComboBox = CType(sender, ComboBox)
        '項目に表示する文字列
        Dim txt As String
        If e.Index > -1 Then
            txt = cmb.Items(e.Index).ToString()
        Else
            txt = cmb.Text
        End If
        '使用するフォント
        Dim f As New Font(txt, cmb.Font.Size)
        '使用するブラシ
        Dim b = New SolidBrush(e.ForeColor)
        '文字列を描画する
        Dim ym As Single = _
            (e.Bounds.Height - e.Graphics.MeasureString(txt, f).Height) / 2
        e.Graphics.DrawString(txt, f, b, e.Bounds.X, e.Bounds.Y + ym)
        f.Dispose()
        b.Dispose()
        'フォーカスを示す四角形を描画
        e.DrawFocusRectangle()
    End Sub


    Private Sub txt_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDefaultLimit.KeyPress
        If e.KeyChar < "0"c OrElse e.KeyChar > "9"c Then
            e.Handled = True
        End If
    End Sub

    Private Sub cboFont_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Settings.FontName = cboFont.Text
        Settings.Apply(txtSample)
    End Sub

    Private Sub txtFontSize_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Settings.FontSize = txtFontSize.Value
        Settings.Apply(txtSample)
    End Sub

    Private Sub chkBold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Settings.FontBold = chkBold.Checked
        Settings.Apply(txtSample)
    End Sub

    Private Sub chkItalic_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Settings.FontItalic = chkItalic.Checked
        Settings.Apply(txtSample)
    End Sub

    Private Sub cmdForeColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ColorDlg.Color = cmdForeColor.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            Settings.ForeColor = ColorDlg.Color
            cmdForeColor.BackColor = ColorDlg.Color
            cmdForeColor.ForeColor = Common.ReversedColor(ColorDlg.Color)
            Settings.Apply(txtSample)
        End If
    End Sub

    Private Sub cmdBackColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ColorDlg.Color = cmdBackColor.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            Settings.BackColor = ColorDlg.Color
            cmdBackColor.BackColor = ColorDlg.Color
            cmdBackColor.ForeColor = Common.ReversedColor(ColorDlg.Color)
            Settings.Apply(txtSample)
        End If
    End Sub

    Private Sub chkShowData_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkShowData.CheckedChanged
        Settings.ShowData = chkShowData.Checked
    End Sub

    Private Sub chkShowAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkShowAll.CheckedChanged
        Settings.ShowAll = chkShowAll.Checked
    End Sub

    Private Sub txtDefaultLimit_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDefaultLimit.Validated
        Settings.SetDefaultLimit(txtDefaultLimit.Text)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Settings.Refresh()
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Settings.SaveConfig()
        Me.Close()
    End Sub
End Class